package org.hs.phd.odi.tracking;

import java.awt.Polygon;
import java.awt.Rectangle;

import org.hs.phd.common.simpleplayer.FramePlayer;
import org.hs.phd.common.simpleplayer.FrameSource;
import org.hs.phd.common.simpleplayer.ImageSequenceFrame;
import org.hs.phd.common.simpleplayer.PlayerFilter;
import org.hs.phd.common.simpleplayer.SimplePlayerFramework;
import org.hs.phd.common.simpleplayer.filter.CropFilter;
import org.hs.phd.common.simpleplayer.filter.DownSampleFilter;
import org.hs.phd.common.simpleplayer.framesource.FolderFrameSource;
import org.hs.phd.odi.tracking.tracker.Tracker;
import org.hs.phd.odi.tracking.tracker.TrackerUtils;
import org.hs.phd.odi.tracking.tracker.comaniciu.ComaniciuTracker;
import org.hs.phd.odi.tracking.tracker.histogram.Weighted3DHistogram;
import org.hs.phd.odi.tracking.tracker.kernel.EpanechnikovKernel;
import org.hs.phd.odi.tracking.tracker.similaritymeasure.BhattacharyaCoefficientSimilartyMeasure;
import org.hs.phd.odi.tracking.tracker.trackwindow.RegularGridRectangularTrackWindow;


public class TrackingDemo1 {

	static class TrackerFilter implements PlayerFilter{
		
		private Tracker tracker;
		private boolean firstTime;
		private final Polygon initialTrackShape;

		public TrackerFilter(Tracker tracker, Polygon initialTrackShape){
			this.tracker = tracker;
			this.initialTrackShape = initialTrackShape;
			firstTime = true;
		}

		@Override
		public void filter(ImageSequenceFrame input) {
			if(firstTime){
				firstTime = false;
				tracker.init(initialTrackShape, input.getFrameImage());
			}
        	tracker.track(input.getFrameImage());
		}
		
	}
	
    public static void main(String[] args) throws Exception{

        ComaniciuTracker tracker = new ComaniciuTracker(new Weighted3DHistogram(32, 32, 16, new EpanechnikovKernel()), 
				new BhattacharyaCoefficientSimilartyMeasure(), new RegularGridRectangularTrackWindow());
        tracker.setVisulizationEnabled(true);
        tracker.setVisualizationWaitMillis(1);

        //FrameSource frameSource = initBlackCarOnTopMiddle(tracker);
        //FrameSource frameSource = initBrownCarOnLeft(tracker);
        FramePlayer player = initWhiteCarOnLeft(tracker);
        
        player.play();
        
    }

	private static FramePlayer initBlackCarOnTopMiddle(ComaniciuTracker tracker) {
		return initTracker(tracker, 4151, new Rectangle(241, 58, 32, 13));
	}

	private static FramePlayer initBrownCarOnLeft(ComaniciuTracker tracker) {
		return initTracker(tracker, 4196, new Rectangle(67, 155, 25, 30));
	}

	private static FramePlayer initWhiteCarOnLeft(ComaniciuTracker tracker) {
		return initTracker(tracker, 4380, new Rectangle(163, 79, 30, 20));
	}

	private static FramePlayer initTracker(ComaniciuTracker tracker, int startFrameNo, Rectangle trackStartRect) {
		FrameSource frameSource = new FolderFrameSource(
                "D:\\Hakan_PhD\\Data\\OwnData\\KAYIT_20111123\\KAYIT_dia6_exp512\\renamed\\Image",
                ".jpg", startFrameNo, 25);
                
        FramePlayer player = SimplePlayerFramework.createPlayer(frameSource);
        player.addFilter( new CropFilter(new Rectangle(205, 85, 1019, 874)) );
        player.addFilter( new DownSampleFilter() );
        
        player.addFilter( new TrackerFilter(tracker, TrackerUtils.getAsPolygon(trackStartRect)) );
        
        player.disableViewer();

        return player;
	}

}
